import { MiddlewareConsumer, Module, NestModule, RequestMethod } from '@nestjs/common';
import { AppController } from './app.controller';
import { AppService } from './app.service';
import { AaaMiddleware } from './aaa.middleware';

@Module({
  imports: [],
  controllers: [AppController],
  providers: [AppService],
})

//继承 NestModule 接口
//实现 NestModule 接口的 configure 方法，在里面应用 AaaMiddleware 到所有路由
export class AppModule implements NestModule{
  configure(consumer: MiddlewareConsumer) {
      // consumer.apply(()=>{ console.log('Function'); }).forRoutes("*")  直接传递函数不注入中间件
      // consumer.apply(AaaMiddleware).forRoutes("*")  所有路由地址都匹配;
      
      consumer.apply(AaaMiddleware).forRoutes({path: 'hello*',method: RequestMethod.GET });
      consumer.apply(AaaMiddleware).forRoutes({path: 'world2',method: RequestMethod.GET });
  }
}
